package com.peanut.baby.mvp.main.expert.qa;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.peanut.baby.R;
import com.peanut.baby.model.QA;
import com.peanut.baby.mvp.profile.ProfileActivity;
import com.peanut.baby.mvp.qadetail.QADetailActivity;
import com.peanut.baby.util.TimeUtil;
import com.peanut.devlibrary.adapter.HolderAdapter;
import com.peanut.devlibrary.imageloader.ImageLoader;

import java.util.List;

/**
 * Created by anonymouslystudio on 2017/7/28.
 */

public class QAListAdapter extends HolderAdapter<QA, QAListAdapter.Holder> {

    private OnQAPraiseClickListener listener;
    public QAListAdapter(Context context, List<QA> listData) {
        super(context, listData);
    }

    public void setOnQAPraiseClickListener(OnQAPraiseClickListener lis){
        this.listener = lis;
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, QA object, int position) {
        return layoutInflater.inflate(R.layout.layout_item_qa_list, null);
    }

    @Override
    public Holder buildHolder(View convertView, QA object, int position) {
        Holder holder = new Holder();
        holder.root = convertView;
        holder.nick = (TextView) convertView.findViewById(R.id.qa_asker_nick);
        holder.avatar = (ImageView) convertView.findViewById(R.id.qa_asker_avatar);
        holder.date = (TextView) convertView.findViewById(R.id.qa_asker_date);
        holder.status = (TextView) convertView.findViewById(R.id.qa_asker_status);
        holder.lisCount = (TextView) convertView.findViewById(R.id.qa_listen_count);
        holder.cmtCount = (TextView) convertView.findViewById(R.id.qa_cmt_count);
        holder.prasiedCount = (CheckBox) convertView.findViewById(R.id.qa_prasied_count);
        holder.content = (TextView) convertView.findViewById(R.id.qa_content);
        holder.category = (TextView) convertView.findViewById(R.id.qa_category);
        return holder;
    }

    @Override
    public void bindViewDatas(final Holder holder, final QA item, int position) {

        ImageLoader.getInstance().loadImageView(context, item.creatorAvatar, holder.avatar);
        holder.nick.setText(item.creatorNickname);
        holder.date.setText(TimeUtil.getQAListDisplayTime(item.createTime));
        holder.status.setText(item.getStatusString());
        holder.lisCount.setText(item.listenCount + "");
        holder.cmtCount.setText(item.commentCount + "");
        holder.prasiedCount.setText(item.praiseCount + "");
        holder.content.setText(item.description);
        holder.prasiedCount.setChecked(item.isPraised());
        holder.category.setText(item.type);

        if(item.isAnswered()){
            holder.status.setTextColor(context.getResources().getColor(R.color.colorPrimary));
        }else{
            holder.status.setTextColor(context.getResources().getColor(R.color.text_dark));
        }

        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QADetailActivity.start(context,item.id);
            }
        });

        holder.prasiedCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.prasiedCount.setChecked(item.isPraised());
                if (listener != null) {
                    listener.onQAPraiseClick(item);
                }
            }
        });

        holder.avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProfileActivity.start(context,item.creatorId+"");
            }
        });


    }

    class Holder {
        View root;
        TextView nick;
        ImageView avatar;
        TextView date;
        TextView status;
        TextView lisCount;
        TextView cmtCount;
        CheckBox prasiedCount;
        TextView content;
        TextView category;
    }

    public interface OnQAPraiseClickListener{
        void onQAPraiseClick(QA qa);
    }
}
